angular.module('service.order', [
        "ngResource",
        "ngStorage"
    ])
    .service('OrderService', OrderService);
OrderService.$inject = ['$resource', '$localStorage','AuthUserService'];
function OrderService ($resource, $localStorage,AuthUserService) {
    var $storage = $localStorage.$default({
        xwmorder: '',
        xwmorderid:'',
    });
    var order_res = $resource(mallorder + 'orders/:id');
    //创建订单
    this.createOrder = function (order, success_func, error_func) {
        var resource = $resource(mallorder + 'orders/place');
        return resource.save({}, order, success_func, error_func);
    };
    //通过用户id获取订单
    this.getOrderByUid = function(status){
        var userid = AuthUserService.getUserByIdCookies();
        var orderList = $resource(mallorder + 'orderlist/bybuyid/:buyid/page');
        return orderList.query({
            buyid: userid,
            status: status.status,
            paystate: status.paystate,
            type: status.type,
            num: status.num,
            n: status.n
        });
    };
    //通过订单id获取订单详情
    this.getOrderByOrderId = function(orderid){
        var order = $resource(mallorder + 'orders/byorderid/:orderid');
        return order.get({orderid:orderid});
    };
    //缓存订单信息
    this.cacheOrder = function (order) {
        $storage.xwmorder = order;
        return $storage.xwmorder;
    };
    // 查询订单详情
    this.getOrderById = function (orderid) {
        var orderinfo = order_res.get({id: orderid});
        return orderinfo;
    };
    // 通过id更新订单
    this.updateOrderById = function (order,success,error) {
        var orderinfo = order_res.save({id: order.orderid},order,success,error);
        return orderinfo;
    };
    //通过id删除订单
    this.delOrderById = function(orderid,success,error){
        var orderinfo = order_res.remove({id: orderid},{},success,error);
        return orderinfo;
    };
    this.getCacheOreder = function () {
        return $storage.xwmorder;
    };
    this.delcache = function () {
        $storage.xwmorder = '';
    };

    //缓存订单id信息
    this.cacheOrderid = function (orderid) {
        $storage.xwmorderid = orderid;
        return $storage.xwmorderid;
    };

    this.getCacheOrederid = function () {
        return $storage.xwmorderid;
    };
    this.delcacheid = function () {
        $storage.xwmorderid = '';
    };
    //计算预送达时间数组
    var _self = this;
    this.sendTimeList = function(startTime,endTime,fastestTime){
        var myDate = new Date();
        var date = myDate.toLocaleDateString();
        date = date.replace(/\//g,"-");
        var hours = myDate.getHours();
        var Minutes = myDate.getMinutes();
        var Seconds = myDate.getSeconds();//获取当前的时、分、秒
        var nowTime = (60*60*hours+60*Minutes+Seconds)*1000;//当前的秒数
        var sendTimeList = [],sendTime,last_time,text,sendInfo;
        if(!startTime&&!endTime){
            sendTime = nowTime+fastestTime;
            text = "尽快送达|预计"+this.changeTimestamp(sendTime);
            sendInfo = {
                "date":date,
                "time":sendTime,
                "text":text,
            };
            sendTimeList.push(sendInfo);
            return sendTimeList;
        }
        if(nowTime<startTime){
            sendTime = startTime+fastestTime;
            text = "尽快送达|预计"+this.changeTimestamp(sendTime);
            sendInfo = {
                "date":date,
                "time":sendTime,
                "text":text,
            };
            sendTimeList.push(sendInfo);
            last_time = lastTime(sendTime);
            setTimeList(sendTimeList,last_time,endTime,date)
        }else{
            sendTime = nowTime+fastestTime;
            text = "尽快送达|预计"+this.changeTimestamp(sendTime);
            sendInfo = {
                "date":date,
                "time":sendTime,
                "text":text,
            };
            sendTimeList.push(sendInfo);
            last_time = lastTime(sendTime);
            setTimeList(sendTimeList,last_time,endTime,date)
        }
        return sendTimeList;
    };
    //时间戳转化成小时分钟
    this.changeTimestamp = function(Timestamp,second){
        //var newDate = new Date(Timestamp);
        var Hours = parseInt(Timestamp/(1000*60*60));
        var Seconds = Timestamp%(1000*60*60);
        var Minutes = parseInt(Seconds/(60*1000));
        if(Minutes < 10){
            Minutes = "0"+ Minutes;
        }
        var _date = Hours + ':'+Minutes;
        if(second){
            _date = _date+":00";
        }
        return _date;
    };
    //得到下一个最近的整十分钟的时间
    function lastTime(Timestamp){
        var Hours = parseInt(Timestamp/(1000*60*60));
        var Seconds = Timestamp%(1000*60*60);
        var Minutes = parseInt(Seconds/(60*1000));
        var lastTime = Math.ceil(Minutes/10)*10*60*1000;
        var overTime = lastTime- Seconds;
        var _date = Timestamp+overTime;
        return _date;
    }
    //获取配送时间区间列表
    function setTimeList(sendTimeList,last_time,endTime,date){
        for(var i=1;i<100;i++){
            var sendTimeMax = last_time+20*60*1000;
            if(sendTimeMax>endTime){
                break;
            }
            var text =_self.changeTimestamp(last_time)+"-"+_self.changeTimestamp(sendTimeMax);
            var sendInfo = {
                "date":date,
                "time":last_time,
                "text":text,
            };
            sendTimeList.push(sendInfo);
            last_time = sendTimeMax;
        }
    }
    //到店取货时间列表获取函数
    this.setDateList = function(dateLength){
        var dateList = [];
        var date = new Date();
        var weekDayList=new Array(7);
        weekDayList[0]="星期日";
        weekDayList[1]="星期一";
        weekDayList[2]="星期二";
        weekDayList[3]="星期三";
        weekDayList[4]="星期四";
        weekDayList[5]="星期五";
        weekDayList[6]="星期六";
        for(var i=0;i<dateLength;i++){
            var targetdays=date.getTime() + 1000*60*60*24*i;
            var newDate = new Date(targetdays),year = newDate.getFullYear(),month = newDate.getMonth() + 1,day = newDate.getDate(),weekDay,today,choose;
            if(i==0){
                weekDay = "今天";
                today = true;
                choose = true;
            }else if(i==1){
                weekDay = "明天";
                today = false;
                choose = false;
            }else if(i==2){
                weekDay = "后天";
                today = false;
                choose = false;
            }else{
                weekDay = weekDayList[newDate.getDay()];
                today = false;
                choose = false;
            }
            var dateContent = {
                "date":getDateString(year,month,day),
                "weekDay":weekDay,
                "today":today,
                "choose":choose
            };
            dateList.push(dateContent);
        }
        return dateList;
    };
    function getDateString(year,month,day){
        var dateString = year+"-"+month+"-"+day;
        return dateString;
    }
    //获取配送时间区间列表
    this.setTimeList = function(startTime,endTime,date){
        var sendTimeList = [];
        var nowTime;
        if(date.today){
            var myDate = new Date();
            var hours = myDate.getHours();
            var Minutes = myDate.getMinutes();
            var Seconds = myDate.getSeconds();//获取当前的时、分、秒
            nowTime = (60*60*hours+60*Minutes+Seconds)*1000;//当前的秒数
        }
        var intervalTime = 15*60*1000;
        for(var i=1;i<100;i++){
            var sendTimeMax = startTime+intervalTime;
            if(sendTimeMax>endTime){
                break;
            }
            var text =this.changeTimestamp(startTime)+"-"+this.changeTimestamp(sendTimeMax);
            var disabled = false;
            var choose = false;
            if(nowTime){
                if(nowTime>=startTime){
                    disabled = true;
                    //choose = true;
                }/*else{
                 if(startTime - nowTime<intervalTime){
                 choose = true;
                 }
                 }*/
            }
            var sendInfo = {
                "time":startTime,
                "text":text,
                "disabled":disabled,
                "choose":choose
            };
            sendTimeList.push(sendInfo);
            startTime = sendTimeMax;
        }
        return sendTimeList;
    };
    /*手动取消订单*/
    this.cancelorderbyid = function(id,success,error){
        var resource = $resource(mallorder + 'orders/cancel/:id');
        return resource.save({id:id},{},success,error);
    }
}